<?php
namespace Home\Controller;
use Think\Controller;
class UserController extends Controller {
     Public function _initialize(){
        R("Public/check_login");
        //会员信息
        $user=getUserInfo();
        $this->assign('user',$user);
     }
     public function index(){
         $uid=is_login();
         $tab=I('get.tab','no_pay');
         $condition=D("user")->order_condition($tab);
         $list=M("Oorder")->select();
         foreach ($list as &$value) {
              $value['payway']=getPayName($value['od_payway']);
              $value['sendway']=getSendName($value['od_sendway']);
         }
         $config=array(
           'tittle'=>'会员中心',
         );
         $num=D("user")->order_num();
         $this->assign('config',$config);
         $this->assign('list',$list);
         $this->assign('num',$num);
         $this->display();
       
      } 
    /**
     * [order_detail 订单详情]
     * @return [type] [description]
     */
    public function order_detail(){
         if($_GET['id']){
            $id=I("get.id"); 
         }else{
            $this->error('无选中订单');
         }
         $order=M("Oorder")->where(array("od_id"=>$id))->find();
         $order['od_address']=json_decode($order['od_address'],true);
         //dump($order);

         //购物车商品信息
         //$return=getShop();
         $orderGoods=M('OrderGoods')
               ->field(array('odg_json','odg_total','od_id'))
               ->where(array('od_id'=>$id))
               ->find();
         $goods=json_decode($orderGoods['odg_json'],true);
         $total=$orderGoods['odg_total'];



         
         $config=array(
           'tittle'=>'我的订单',
         );
         $this->assign("data",$order);
         $this->assign('config',$config);
         $this->assign("goods",$goods);
         $this->assign('total',$total);
         $this->display();
    }
    /**
     * [myorder 我的订单]
     * @return [type] [description]
     */
    function myorder(){
        $uid=is_login();
        $tab=I('get.tab','no_pay');
        $condition=D("user")->order_condition($tab);
        $list=M("Oorder")->where($condition)->select();
        $config=array(
           'tittle'=>'会员中心',
        );
        $num=D("user")->order_num();
        $this->assign('config',$config);
        $this->assign('list',$list);
        $this->assign('num',$num);
        $this->display();
    } 
    //我的关注
    public function my_watch(){
         $mgoods=M('goods');
         $count=$mgoods->where($map)->count();
         $limit=R('Public/fenpage',array($count,'12','page'));
         $gList=$mgoods
              //->where($map)
              ->field(array('go_name','go_value','go_id','go_pics'))
              ->limit($limit)
              ->select();
         foreach ($gList as &$value) {
            $value['go_pics']=getalbumface($value['go_pics']);
         }
         $this->assign('glist',$gList);
         $this->display();
    }
     //我的浏览记录
    public function my_view(){
         $mgoods=M('goods');
         $count=$mgoods->where($map)->count();
         $limit=R('Public/fenpage',array($count,'12','page'));
         $gList=$mgoods
              //->where($map)
              ->field(array('go_name','go_value','go_id','go_pics'))
              ->limit($limit)
              ->select();
         foreach ($gList as &$value) {
            $value['go_pics']=getalbumface($value['go_pics']);
         }
         $this->assign('glist',$gList);
         $this->display();
    }
    /**
     * [my_address 我的收获地址]
     * @return [type] [description]
     */
	function my_address(){
		 $find=$this->isinfo();
		 //存在记录
		 if($find){
             $address=$find['ui_address'];
             $address=json_decode($address,true);
             foreach ($address as &$value) {
                 $value['area']=getAdName($value['area'][0]).getAdName($value['area'][1]).getAdName($value['area'][2]);
             }
             
		 }
		 $this->assign('address',$address);
         $this->display();
	}
	/**
	 * [doaddress 处理地址]
	 * @return [type] [description]
	 */
	function doaddress(){
		if($this->isinfo()){
			$post=$this->isinfo();
			$post=json_decode($post['ui_address'],true);
		}
        $array=array(
             'receiver'=>$_POST['receiver'],
             'area'=>array($_POST['province'],$_POST['city'],$_POST['area']),
             'address'=>$_POST['address_detail'],
             'mobile'=>$_POST['mobile'],
             'email'=>$_POST['email'],
        );
        if($_POST['id']){
            $id=$_POST['id']-1;
            $post[$id]=$array;
        }else{
            $post[]=$array;   
        }
       
       $data['ui_address']=json_encode($post);
       $this->actioninfo($data);
	}
    /**
     * [editaddress 编辑三级联动地址]
     * @return [type] [description]
     */
    function editaddress(){
         $id=I('id')-1;
         $uid=session('user_id');
         $find=$this->isinfo();
         //获取地址
         $address=$find['ui_address'];
         $address=json_decode($address,true);
         $address=$address[$id];
         ajaxsuccess($address,$msg='成功获取数据');
    }
	/**
	 * [isinfo 判断是否已经在会员信息分表里有记录]
	 * @param  [type] $uid [description]
	 * @return [type]      [description]
	 */
	function isinfo(){
        $uid=session('uid');
        $find=M('UserInfo')
              ->where(array('user_id'=>$uid))
              ->find();
        if($find)return $find;
        else return false;
	}
    /**
     * [actioninfo 对会员信息分表处理]
     * @return [type] [如果有的话编辑，无的话插入]
     */
	function actioninfo($data){
        $uid=session('uid');
        $map=array('user_id'=>$uid);
        $model=M('UserInfo');
        $count=$model
              ->where($map)
              ->select();
        //编辑或者增加
        if($count){
        	$re=edit($data,$map,$model);
        }else{
        	$data['user_id']=$uid;
        	$re=add($data,$model);
        }

        if($re){
            	ajaxsuccess();
            }else{
            	ajaxerror();
        }
        
	}

    public function shiyan(){
       $array=array(1,2,3);
       $a=array_walk($array,"shiyan");
       echo $a;
    }
}